module Robot
  class << self
    def send_all(content)
      url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=ae238d04-eabb-492f-80f6-055592a1bc7c"
      robot_send url, content
    end

    def zhangchunhua(content)
      url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=29fd4d15-50b5-40ef-9be9-7505badb7741"
      robot_send url, content
    end

    def send_alert(content)
      url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=8bd51efe-4eb1-48ce-8e37-abcbcb569064"
      robot_send url, content
    end

    def send_index(content)
      url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=f0eef187-5740-4312-bed5-fd27f1de2d2d"
      robot_send url, content
    end

    def send_block(content)
      url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=8e0b0933-a0a9-4e7a-be85-568e9fb614b5"
      robot_send url, content
    end

    def send_held(content)
      url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=4d90d896-ea0d-4f3c-b620-1f22b1b0118e"
      robot_send url, content
    end

    def send_init(content)
      url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=a2390a27-18a8-4876-b1d3-0b46ef52a93d"
      robot_send url, content
    end

    def send_heart(content)
      url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=4e4ffe64-54ae-48e3-b8b3-51999376d7f7"
      robot_send url, content
    end

    # 钉钉机器人 https://open.dingtalk.com/document/robots/custom-robot-access
    def dd_fluctuation(content)
      timestamp = (Time.now.to_f * 1000).to_i.to_s
      sign = dd_sign 'SEC0e8c9dbeb9a31b2099815a8cf529c5382955073dc95966c2de76426b601c6fb5', timestamp
      url = "https://oapi.dingtalk.com/robot/send?access_token=3ad3bf40848d513a37c8c2d4a4ee084a53eef58123aebf2e71c1ecd71f105adb&timestamp=#{timestamp}&sign=#{sign}"
      robot_send url, content
    end
    def dd_fluctuation_ai(content)
      timestamp = (Time.now.to_f * 1000).to_i.to_s
      sign = dd_sign 'SECdf20150754d2e6b31e4ecd8b89f7b591f6c0344bd1e590a131588a67c44b1cfe', timestamp
      url = "https://oapi.dingtalk.com/robot/send?access_token=919d29df2c16da900f77e59af3dd3818cffa638bc9e8463f0992ec9d47a4c944&timestamp=#{timestamp}&sign=#{sign}"
      robot_send url, content
    end

    def dd_must_do(content)
      timestamp = (Time.now.to_f * 1000).to_i.to_s
      sign = dd_sign 'SEC75506ea6bb62ab9db1cb3d88d29be992b3416c048fa3f0745dab568c50e6919f', timestamp
      url = "https://oapi.dingtalk.com/robot/send?access_token=0aacf14714f5e66f666f99876e82288f0effe109a918a217e7165e798b45d98b&timestamp=#{timestamp}&sign=#{sign}"
      robot_send url, content
    end
    def dd_must_do_ai(content)
      timestamp = (Time.now.to_f * 1000).to_i.to_s
      sign = dd_sign 'SECd446b08adf178e3008cf4b27f5a1980118ae5632a947d1a6a9fba1c742525e46', timestamp
      url = "https://oapi.dingtalk.com/robot/send?access_token=854cce46dd951688696fbd4579380ed2341d096f733954147dd7623cf0320cbc&timestamp=#{timestamp}&sign=#{sign}"
      robot_send url, content
    end
    def dd_held(content)
      timestamp = (Time.now.to_f * 1000).to_i.to_s
      sign = dd_sign 'SECe14a20b8b2d4d5d02f1a0b0a28a7ad03e6fd2fb4d90c59c9bf51e37cd435f97c', timestamp
      url = "https://oapi.dingtalk.com/robot/send?access_token=e9f064257b4046818707fb514f397425ff30df30951cbd3596f4048cb20fd6c9&timestamp=#{timestamp}&sign=#{sign}"
      robot_send url, content
    end

    def dd_held_ai(content)
      timestamp = (Time.now.to_f * 1000).to_i.to_s
      sign = dd_sign 'SEC7915d1471ea215a16b10806b5c685c7ac08b873833f5c655ceedf9b0f488b38d', timestamp
      url = "https://oapi.dingtalk.com/robot/send?access_token=8c7d8fa8d130ebf72a01ddb10861257965ff7ea6bab17fe9cedf70fe9a177226&timestamp=#{timestamp}&sign=#{sign}"
      robot_send url, content
    end

    def dd_choice(content)
      timestamp = (Time.now.to_f * 1000).to_i.to_s
      sign = dd_sign 'SEC318cc02529ab53ad70c9ee86a748fb8967662b00ad14813f7be67204914a6a7c', timestamp
      url = "https://oapi.dingtalk.com/robot/send?access_token=afdc01f26ec53261f21a80ec0356a69cae82fdfc4d6695ea825d073a010c566c&timestamp=#{timestamp}&sign=#{sign}"
      robot_send url, content
    end
    def dd_choice_ai(content)
      timestamp = (Time.now.to_f * 1000).to_i.to_s
      sign = dd_sign 'SEC929dcdf98435651fc19230beb0207f30bf78873ba71d6ba5024b46f9cbc1bba2', timestamp
      url = "https://oapi.dingtalk.com/robot/send?access_token=00673eb97d080f5aed717ecea4a40bf00b6e6ee6fc3304113c67a1b12dd1e501&timestamp=#{timestamp}&sign=#{sign}"
      robot_send url, content
    end

    def dd_long(content)
      timestamp = (Time.now.to_f * 1000).to_i.to_s
      sign = dd_sign 'SEC02968026522a598058bbf94339fffb38894b8cd6b30246a90deb77daa119ec0c', timestamp
      url = "https://oapi.dingtalk.com/robot/send?access_token=6b7a77ecc5c8df6c9957587720803433c690cb952bf9120da25b205dd315545e&timestamp=#{timestamp}&sign=#{sign}"
      robot_send url, content
    end
    def dd_long_ai(content)
      timestamp = (Time.now.to_f * 1000).to_i.to_s
      sign = dd_sign 'SEC460826adc726593b3921ede8ad8cf559662b988eb06e7a12aa179a32758b9741', timestamp
      url = "https://oapi.dingtalk.com/robot/send?access_token=5e6d5b30439630545e2e3ae147eada85e67b6ad466430f1f1033aa953974cb78&timestamp=#{timestamp}&sign=#{sign}"
      robot_send url, content
    end

    private def robot_send(url, content)
      @conn = Faraday.new(url: url) do |f|
        f.request :json
        f.response :json
        f.adapter Faraday.default_adapter
      end
      if url.include?("dingtalk")
        payload = {
          msgtype: "markdown",
          markdown: {
            title: "title",
            text: content
          }
        }
      end
      if url.include?("weixin")
        payload = {
          msgtype: "markdown",
          markdown: {
            content: content
          }
        }
      end
      @conn.post do |req|
        req.headers['Content-Type'] = 'application/json'
        req.body = payload.to_json
      end
    end
  end
end

def dd_sign(secret, timestamp)
  secret_enc = secret.encode('utf-8')
  string_to_sign = "#{timestamp}\n#{secret}".encode('utf-8')
  hmac_code = OpenSSL::HMAC.digest(OpenSSL::Digest.new('sha256'), secret_enc, string_to_sign)
  URI.encode_www_form_component(Base64.strict_encode64(hmac_code))
end

# content = <<~MD
#   **服务器状态报告**
#   > 时间: #{Time.now.strftime("%F %T")}
#   >
#   > - CPU负载: `82%`
#   > - 内存使用: `4.5/8 GB`
#   > - 磁盘空间: `120/256 GB`
#   >
#   > [立即查看](https://example.com/monitor)
# MD
# Robot.send_all content

# payload = {
#   "msgtype": "markdown",
#   "markdown": {
#     "title":"杭州天气",
#     "text": "#### 杭州天气 @150XXXXXXXX \n > 9度，西北风1级，空气良89，相对温度73%\n > ![screenshot](https://img.alicdn.com/tfs/TB1NwmBEL9TBuNjy1zbXXXpepXa-2400-1218.png)\n > ###### 10点20分发布 [天气](https://www.dingtalk.com) \n"
#   },
#   "at": {
#     "atMobiles": [
#       "150XXXXXXXX"
#     ],
#     "atUserIds": [
#       "user123"
#     ],
#     "isAtAll": false
#   }
# }

